import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/bean/user.dart';
import 'package:app/src/http/model/submit_model.dart';
import 'package:app/src/manager/audit_manager.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/image/ui_image_upload.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../../http/bean/worksdo/hardware_return_bean.dart';
import '../../../../../../http/model/hardware_return_model.dart';
import '../../../../../../http/model/material_requisition_model.dart';
import '../../../../works/public/review_submitted.dart';

///审核提交
class ReviewSubmittedStateful3 extends StatefulWidget {
  //是否提交
  var hasSubmit = true;

  //是否包含保存
  var hasSave = false;

  //什么操作都没有；只有一个返回按钮。
  var onlyBack = false;

  //数据
  AppHttpResult<String, String>? mDatas;

  //提交之前回调,用于提交参数SubData赋值。
  ResponseCallbackWithT<SubData>? _callbackWithT = null;

  //提交前；用于验证数据。
  ResponseCallbackWithBoolResultII? _callbackWithBoolResultII = null;

  HardwareReturnSubData? mSubData;

  ReviewSubmittedStateful3(this.mDatas, this.mSubData,
      {bool? hasSave = false,
      bool? hasSubmit = true,
      bool? onlyBack = false,
      ResponseCallbackWithT<SubData>? callbackWithT = null,
      ResponseCallbackWithBoolResultII? callbackWithBoolResult = null}) {
    this.hasSave = hasSave ?? false;
    this.hasSubmit = hasSubmit ?? true;
    this.onlyBack = onlyBack ?? false;
    this._callbackWithT = callbackWithT;
    this._callbackWithBoolResultII = callbackWithBoolResult;
  }

  @override
  _State createState() => _State();
}

class _State extends State<ReviewSubmittedStateful3> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    presenter?.mDatas = widget?.mDatas;
    presenter?.mSubData = widget.mSubData;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    AuditManager.getAuditManager()
        .setDetailsWithAuto(false, "review_submitted.dart"); //fixme 不在审核详情页面。
  }

  @override
  Widget build(BuildContext context) {
    // AuditManager.setDetailsWithAuto(
    //     true, "review_submitted.dart"); //fixme 当前在审核详情页面。现在需求任然需要显示通知信息。。
    return _body(context);
  }

  Widget _body(BuildContext context) {
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      presenter.setStateBody = setState;
      List<Widget> children = [];
      var topMargin = 8.0;
      if (widget.onlyBack) {
        ///fixme 什么操作都没有，只有一个返回按钮。
        var row = Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
                child: MaterialButton(
              onPressed: () {
                AppRouteHelper.pop(context);
              },
              color: ColorUtils.parseColor("#8F97AC"),
              textColor: Colors.white,
              padding: EdgeInsets.all(15),
              elevation: 0,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(8),
                      bottomLeft: Radius.circular(8),
                      topRight: Radius.circular(8),
                      bottomRight: Radius.circular(8))),
              child: Text(AppStrings.of(context).ReturnsList),
            ))
          ],
        );
        children.add(row);
      } else {
        //var _data = presenter?.mSubData?.hardwareReturnListBean;
        var _datas = presenter?.mSubData?.datas;
        //isRefreshPreviousAppList = true; //fixme true返回上一个页面，列表会刷新。
        var isReturnAll = true; //是否整单退库。
        _datas?.forEach((element) {
          if (!HardwareReturnListBean.isReturnAll2(element.Status)) {
            isReturnAll = false;
          }
        });
        // var submitName = AppStrings.of(context).WithdrawalfromTreasury; //退库
        // var submitName =
        //     widget.mSubData?.hardwareReturnListBean?.getGoName(context) ?? "";
        var submitName = AppStrings.of(context).WithdrawalfromTreasury; //退库
        if (isReturnAll) {
          submitName = AppStrings.of(context).WithdrawalfromTreasury2; //整单退库
        }
        var SubmitAudit = MaterialButton(
          onPressed: () {
            // var isReturnAll =
            //     presenter?.mSubData?.hardwareReturnListBean?.isReturnAll() ??
            //         false;
            if (isReturnAll) {
              //整单退;整架退库
              var content = AppStrings.of(context)
                  .zhengdantuiku; //该物料尚未出库，退库将直接退回至仓库，是否确认退库？
              var okText = AppStrings.of(context).querentuik; //确认退库
              AppDialog.showDialogWithAlert(context,
                      content: content, okText: okText)
                  ?.then((value) {
                if (value ?? false) {
                  go(true);
                }
              });
            } else {
              //单个退
              go(false);
            }
          },
          color: ColorUtils.parseColor("#5985F0"),
          textColor: Colors.white,
          padding: EdgeInsets.all(15),
          elevation: 0,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(8), bottomLeft: Radius.circular(8))),
          child: Text(submitName),
        );
        //关闭
        var ReturnsList = MaterialButton(
          onPressed: () {
            AppRouteHelper.pop(context);
          },
          color: ColorUtils.parseColor("#8F97AC"),
          textColor: Colors.white,
          padding: EdgeInsets.all(15),
          elevation: 0,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
                  topRight: Radius.circular(8),
                  bottomRight: Radius.circular(8))),
          child: Text(AppStrings.of(context).Off),
        );
        var row = Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(child: SubmitAudit),
            Expanded(child: ReturnsList)
          ],
        );
        children.add(row);
      }
      return Container(
        padding: EdgeInsets.only(left: 8, right: 8, top: topMargin, bottom: 16),
        child: Column(
          children: children,
        ),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
                color: Colors.black, blurRadius: 10, offset: Offset(0, 10))
          ],
        ),
      );
    });
  }

  //提交
  go(bool isReturnAll) {
    var b = true;
    if (widget._callbackWithBoolResultII != null) {
      var b2 = widget._callbackWithBoolResultII?.call(); //fixme 提交之前验证数据格式。
      if (b2 != null) {
        b = b2;
      }
    }
    if (b) {
      presenter?.contractapply(context, isReturnAll, widget._callbackWithT);
    }
  }
}

final String _status_submit = "1"; //提交
final String _status_save = "0"; //保存

class _Presenter {
  ///body视图状态
  StateSetter? setStateBody;

  TextEditingController? textEditingController = null;

  //审核状态
  var _status = _status_submit;

  //审核意见
  String auditOpinion = "";

  //数据
  AppHttpResult<String, String>? mDatas;

  //五金退库提交数据
  HardwareReturnSubData? mSubData;

  //提交数据
  SubData? _subData = null;

  ///提交审核
  contractapply(BuildContext context, bool isReturnAll,
      ResponseCallbackWithT<SubData>? _callbackWithT) {
    AuditManager.getAuditManager().setDetailsWithAuto(true,
        "review_submitted2.dart"); //fixme 在审核详情页面,正在提交的时候，不要显示通知信息。没有提交的时候，还是要显示的。
    if (_subData == null) {
      _subData = SubData();
    }
    _callbackWithT?.call(_subData!); //fixme 提交之前回调，用于提交参数赋值。
    //fixme 审核提交
    HardwareReturnModel.MaterialPickingReturn(
            context, isReturnAll, mSubData, true)
        ?.then((value) {
      isRefreshPreviousAppList = true; //fixme true返回上一个页面，列表会刷新。
      if (value.isSuccessForCode()) {
        Toast.showSuccess(
            AppStrings.of(context).SubmitSuccessfully, context); //提交成功
        AppRouteHelper.pop(context, true); //关闭当前审核页面
        AuditManager.getAuditManager().setDetailsWithAuto(
            false, "review_submitted2.dart"); //fixme 审核详情页面提交结束，可以显示通知信息。
      } else {
        //提示错误信息
        AppDialog.showDialogWithInfo(context, content: value.MsgDetail);
        AuditManager.getAuditManager().setDetailsWithAuto(
            false, "review_submitted2.dart"); //fixme 审核详情页面提交结束，可以显示通知信息。
      }
    });
  }
}
